package com.promis.util;

public class StringUtils {

	public final static String DELIMITER_AND = "&";
	public final static String DELIMITER_COMMA = ",";
	public final static String DELIMITER_SEMICOLON = ";";
	public final static String DELIMITER_POINT = ".";
	public final static String DELIMITER_UNDERLINE = "_";

	public static boolean isNullOrEmpty(String str) {
		return str == null || "".equals(str);
	}
	
	public static String replaceLast(String str, String oldstr, String newstr) {
		if (isNullOrEmpty(str)) return str;
		int index = str.lastIndexOf(oldstr);
		if (index == -1) return str;
		
		return str.substring(0, index) + newstr + str.substring(index + oldstr.length(), str.length());
	}

	public static String replacePhone(String phone){
		if (isNullOrEmpty(phone)) return phone;

		return phone.substring(0, 3) + "****" + phone.substring(8, phone.length());
	}

	//15位的幸运码 4位分割
	public static String addSeparate(String str){
		if (isNullOrEmpty(str)) return str;
		int i = 0;
		if(str.length() % 4 == 0) i = str.length() / 4;
		else i = str.length() / 4 + 1;
		String strings = "";
		for(int k = 0; k < i; k++){
			if(k == (i -1))
				strings += str.substring(k * 4, str.length());
			else
				strings += str.substring(k * 4, (k + 1) * 4) + " ";
		}
		return strings;
	}
}
